// 
// Bachelor of Software Engineering
// Media Design School
// Auckland
// New Zealand
//
// (c) 2005 - 2014 Media Design School
//
// File Name	: MenuScreenView.cpp
// Desrciption	: CMenuScreenView implementation file.
// Author		: Hayden Asplet.
// Mail			: hayden.asplet@mediadesignschool.com
//

// PCH
#include "SpaceInvadersStd.h"

// Library Includes

// This Include
#include "MenuScreenView.h"

// Local Includes
#include "Msvc\resource.h"

CMenuScreenView::CMenuScreenView()
	:m_iCurrentSelection(0)
{
	m_background = g_pApp->LoadSprite(IDB_MENUSCREEN);
}

CMenuScreenView::~CMenuScreenView()
{
	g_pApp->m_pGame->DestroyGameObject(m_objectId);
}

void CMenuScreenView::VUpdate(float _fDeltaTime)
{

}

void CMenuScreenView::VRender(float _fDeltaTime)
{
	g_pApp->RenderSprite(m_background, 0, 0);

	CRenderer* pRenderer = g_pApp->m_pGame->GetRenderer();

	if(pRenderer) {
		pRenderer->RenderGameObjects();
	}
}

bool CMenuScreenView::VMsgProc(const CAppMsg& _krMsg)
{
	switch(_krMsg.m_uMsg) {
		case WM_KEYDOWN: {
			// Get the currently attached object.
			CGameObject* pAttachedObject = g_pApp->m_pGame->GetGameObject(m_objectId);

			if(!pAttachedObject) {
			// No object attached.
				return false;
			}
			else {
				shared_ptr<CAnimationComponent> pAnimComponent = pAttachedObject->GetComponent<CAnimationComponent>();

				if(!pAnimComponent) {
				// No animation component, therefore we cannot change selection.
					return false;
				}
				else {
					if(_krMsg.m_wParam == VK_UP || _krMsg.m_wParam == VK_LEFT) {
					// Player wants to select up.
						--m_iCurrentSelection;

						if(m_iCurrentSelection < 0) {
						// We've gone too far up.
							// Wrap the selection for the amount of animations we have.
							m_iCurrentSelection = pAnimComponent->GetAmountOfAnimations() - 1;
						}
					}
					else if(_krMsg.m_wParam == VK_DOWN || _krMsg.m_wParam == VK_RIGHT) {
					// Player wants to select down.
						++m_iCurrentSelection;

						if(m_iCurrentSelection >= pAnimComponent->GetAmountOfAnimations()) {
						// We've gone too far down.
							// Wrap the selection back to the beggining.
							m_iCurrentSelection = 0;
						}
					}
					else if(_krMsg.m_wParam == VK_RETURN || _krMsg.m_wParam == VK_SPACE) {
					// The player has made their selection.
						// Check what the current selection is.
						if(pAnimComponent->GetCurrentAnimation() == "Play") {
							g_pApp->m_pGame->VChangeState(GS_Running);
						}
						else if(pAnimComponent->GetCurrentAnimation() == "Highscores") {
							g_pApp->m_pGame->VChangeState(GS_HighScore);
						}
						else if(pAnimComponent->GetCurrentAnimation() == "Quit") {
							g_pApp->OnClose();
						}
					}

					if(m_iCurrentSelection == 0) {
						pAnimComponent->PlayAnimation("Play", 0, false);
					}
					else if(m_iCurrentSelection == 1) {
						pAnimComponent->PlayAnimation("Highscores", 0, true);
					}
					else {
						pAnimComponent->PlayAnimation("Quit", 0, true);
					}
				}
			}
			break;
		}
		case WM_KEYUP: {
			break;
		}
		case WM_MOUSEMOVE: {

			break;
		}
		case WM_LBUTTONDOWN: {

			break;
		}
		case WM_LBUTTONUP: {

			break;
		}
		case WM_RBUTTONDOWN: {

			break;
		}
		case WM_RBUTTONUP: {

			break;
		}
		default: break;
	}

	return true;
}
